<?php

class Application_Model_MenuMapper
{
   public function __construct()
   {
     $this->menu=new Application_Model_DbTable_Menu();
     $this->orderby='entry_order';
   }

   public function getMenu($name,$module="")
   {
      $menu_name=$name;
      $module_name=$module;

      if($module_name=="")
      {
         $select=$this->menu->select()->where('menu_name=?',"$menu_name")
                                      ->order($this->orderby);
      }
      else
      {
         $select=$this->menu->select()->where('menu_name=?',"$menu_name")
                                      ->where('module_name=?',"$module_name")
                                      ->order($this->orderby);
      }

      $rows=$this->menu->fetchAll($select);

      $menuContainer=new Zend_Navigation();

      foreach ( $rows as $value )
      {
         $menuContainer->addPage(
         Zend_Navigation_Page::factory(array(
            'label' => $value['label'],
            'module' => $value['module_name'],
            'controller' => $value['controller_name'],
            'action' => $value['page_name'],
            'resource' => $value['module_name'].'_'.$value['controller_name'].'_'.$value['page_name']
          )));
      }

      return $menuContainer;
   }


   protected $menu;
   protected $orderby;
}

